import { Controller } from "../../../../mvc/core/Controller";
import { ICopyGroupModuleNew } from "../../../../../modules/interfaces/ICopyGroupModuleNew";
import { CopyGroupModuleNew } from "../../../../../modules/CopyGroupModuleNew";
import { GModuleEvent } from "../../../mvc/GModuleEvent";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { EventName } from "../../../mvc/EventName";
import { NetDispatcher } from "../../../../mvc/core/NetDispatcher";
import { ServerCommand } from "../../../mvc/ServerCommand";
import { CopyGroupGatherPanel } from "../group/CopyGroupGatherPanel";
import { CrossCopyGroupEnterPanel } from "../crossCopyGroup/CrossCopyGroupEnterPanel";
import { EPublicCommand } from "../../../../../Message/Command/EPublicCommand";
import { DataEvent } from "../../../events/DataEvent";
import { GameMapUtil } from "../../../scene/map/GameMapUtil";
import { DataProvider } from "../../../../../fl/data/DataProvider";
import { SPublicMiniPlayer } from "../../../../../Message/Public/SPublicMiniPlayer";
import { EntityUtil } from "../../../scene/layer/utils/EntityUtil";
import { SCopyWaitingInfo } from "../../../../../Message/Public/SCopyWaitingInfo";
import { CopyConfig } from "../../../resource/CopyConfig";
import { WaitingGroupData } from "../group/WaitingGroupData";
import { SWaitingPlayer } from "../../../../../Message/Public/SWaitingPlayer";
import { WaitingPlayerData } from "../group/WaitingPlayerData";
import { CopyUtil } from "../CopyUtil";
import { MessageBlock } from "../../../../../Framework/MQ/MessageBlock";
import { SCopyRoomOperation } from "../../../../../Message/Public/SCopyRoomOperation";
import { ECopyWaitingOperate } from "../../../../../Message/Public/ECopyWaitingOperate";
import { NpcEffectRule } from "../../../rules/NpcEffectRule";
import { GButton } from "../../../../../com/mui/controls/GButton";
import { UiHintIconView } from "../../uiIconBtn/UiHintIconView";
import { LayerManager } from "../../../manager/LayerManager";
import { MsgManager } from "../../../manager/MsgManager";
import { Language } from "../../../../../extend/language/Language";
import { SCopyGroupInfo } from "../../../../../Message/Public/SCopyGroupInfo";
import { GameProxy } from "../../../mvc/GameProxy";
import { SCrossCopyCostNotice } from "../../../../../Message/Public/SCrossCopyCostNotice";
import { SCrossCopyCheckState } from "../../../../../Message/Public/SCrossCopyCheckState";
import { TCopy } from "../../../../../Message/Db/Tables/TCopy";
import { NPCInfo } from "../../../model/NPCInfo";
type int = number;
//class CopyGroupController
    
    export  class CopyGroupController extends Controller
    {
        constructor()
        {
            super();
            return;
        }

        public get copyGroupModule(): ICopyGroupModuleNew
        {
            if (!this._copyGroupModule) 
            {
                this._copyGroupModule = new CopyGroupModuleNew();
                this._copyGroupModule.addEventListener(flash.events.Event.ADDED_TO_STAGE, this.onCopyGroupAddedToStage);
                this._copyGroupModule.addEventListener(GModuleEvent.Event_DoEnterCopy, this.onCopyGroupEnterCopy);
                this._copyGroupModule.addEventListener(GModuleEvent.Event_QuickApply, this.onCopyGroupQuickApply);
                this._copyGroupModule.addEventListener(GModuleEvent.Event_QuickInvite, this.onCopyGroupQuickInvite);
                Dispatcher.addEventListener(EventName.CopyGroupRefreshNearby, this.doRefreshNearbyList);
                NetDispatcher.addCmdListener(ServerCommand.Group_PlayerInfoChange, this.onGroupMemberInfoChange);
                NetDispatcher.addCmdListener(ServerCommand.Group_CaptainChange, this.onGroupMemberInfoChange);
                NetDispatcher.addCmdListener(ServerCommand.CrossGroup_PlayerInfoChange, this.onCrossGroupMemberInfoChange);
                NetDispatcher.addCmdListener(ServerCommand.CrossGroup_CaptainChange, this.onCrossGroupMemberInfoChange);
            }
            return this._copyGroupModule;
        }

        public get copyGroupGatherPanel(): CopyGroupGatherPanel
        {
            if (!this._copyGroupGatherPanel) 
            {
                this._copyGroupGatherPanel = new CopyGroupGatherPanel();
                this._copyGroupGatherPanel.addEventListener(CopyGroupGatherPanel.DoMinimize, this.onGatherPanelMinimize);
            }
            return this._copyGroupGatherPanel;
        }

        public get crossCopyGroupEnterPanel(): CrossCopyGroupEnterPanel
        {
            if (!this._crossCopyGroupEnterPanel) 
            {
                this._crossCopyGroupEnterPanel = new CrossCopyGroupEnterPanel();
            }
            return this._crossCopyGroupEnterPanel;
        }

        protected /* override */ initServer(): void
        {
            NetDispatcher.addCmdListener(ServerCommand.Copy_WaitingInfoCommand, this.onUpdateWaitingInfo);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicCopyOperationRoom, this.onCopyOperationRoomCommand);
            NetDispatcher.addCmdListener(ServerCommand.CrossCopy_WaitingInfoCommand, this.onUpdateCrossWaitingInfo);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicCrossCopyOperationRoom, this.onCopyOperationRoomCommand);
            NetDispatcher.addCmdListener(ServerCommand.CrossCopy_CostNoticeEnter, this.onReceiveCostNoticeEnter);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicCopyCrossCopyCheckState, this.onCrossCopyCheckState);
            Dispatcher.addEventListener(EventName.CopyGroupCrossCopyConfirm, this.onCrossGroupEnterConfirm);
            Dispatcher.addEventListener(EventName.CopyGroupCrossCopyCancel, this.onCrossGroupEnterCancel);
            return;
        }

        /* internal  */onCopyGroupEnterCopy(arg1: DataEvent): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.CopyGroupDoEnterCopy, arg1.data));
            return;
        }

        /* internal  */doRefreshNearbyList(arg1: DataEvent): void
        {
            this.copyGroupModule.doRefreshWaitingRoomInfo();
            return;
        }

        /* internal  */onCopyGroupAddedToStage(arg1: Object /* flash.events.Event */): void
        {
            this.onGroupMemberInfoChange();
            this.onCrossGroupMemberInfoChange();
            return;
        }

        /* internal  */onCrossGroupMemberInfoChange(arg1: Object=null): void
        {
            var loc6=null;
            var loc7=false;
            var loc8=false;
            if (this._copyGroupModule == null || this._copyGroupModule.isHide || !GameMapUtil.isCrossGroupMap()) 
            {
                return;
            }
            var loc1=this._copyGroupModule.copyInfo ? this._copyGroupModule.copyInfo.copyId : 0;
            var loc2=cache.crossGroup.isCaptain;
            var loc3=cache.crossGroup.players;
            var loc4=new DataProvider();
            var loc5=0;
            while (loc5 < loc3.length) 
            {
                loc6 = loc3[loc5] as SPublicMiniPlayer;
                loc7 = EntityUtil.equal(loc6.entityId, cache.role.entityInfo.entityId);
                loc8 = cache.crossGroup.captain && EntityUtil.equal(cache.crossGroup.captain, loc6.entityId);
                loc4.addItem({"type":"", "online": loc6.online, "isCaption": loc8, "captainMode": loc2, "isMySelf": loc7, "player": loc6, "copyId": loc1, "isCross": true});
                ++loc5;
            }
            loc4.sortOn(["isCaption", "online"], Array.DESCENDING);
            this.copyGroupModule.updateGroupMemberList(loc4, loc2, true);
            return;
        }

        /* internal  */onGroupMemberInfoChange(arg1: Object=null): void
        {
            var loc6=null;
            var loc7=false;
            var loc8=false;
            if (this._copyGroupModule == null || this._copyGroupModule.isHide || GameMapUtil.isCrossGroupMap()) 
            {
                return;
            }
            var loc1=this._copyGroupModule.copyInfo ? this._copyGroupModule.copyInfo.copyId : 0;
            var loc2=cache.group.isCaptain;
            var loc3=cache.group.players;
            var loc4=new DataProvider();
            var loc5=0;
            while (loc5 < loc3.length) 
            {
                loc6 = loc3[loc5] as SPublicMiniPlayer;
                loc7 = EntityUtil.equal(loc6.entityId, cache.role.entityInfo.entityId);
                loc8 = cache.group.captain && EntityUtil.equal(cache.group.captain, loc6.entityId);
                loc4.addItem({"type":"", "online": loc6.online, "isCaption": loc8, "captainMode": loc2, "isMySelf": loc7, "player": loc6, "copyId": loc1, "isCross": false});
                ++loc5;
            }
            loc4.sortOn(["isCaption", "online"], Array.DESCENDING);
            this.copyGroupModule.updateGroupMemberList(loc4, loc2, false);
            return;
        }

        /* internal  */onUpdateWaitingInfo(arg1: SCopyWaitingInfo): void
        {
            var loc3=null;
            var loc4=null;
            var loc5=null;
            var loc6=null;
            var loc7=0;
            var loc8=null;
            var loc9=null;
            var loc1=CopyConfig.instance.getInfoByCode(arg1.copyCode);
            if (!loc1) 
            {
                return;
            }
            var loc2=[];
            var loc10=0;
            var loc11=arg1.groupInfos;
            for(loc3 of loc11) 
            {
                if (loc3.playerNum == 5) 
                {
                    continue;
                }
                (loc8 = new WaitingGroupData()).group = loc3;
                loc8.copy = loc1;
                loc8.progress = loc3.progress;
                loc8.playerNum = loc3.playerNum;
                loc8.isCross = false;
                loc2.push(loc8);
            }
            loc4 = [];
            loc7 = 0;
            while (loc7 < arg1.waitingPlayers.length) 
            {
                loc5 = (loc6 = arg1.waitingPlayers[loc7] as SWaitingPlayer).miniPlayer;
                (loc9 = new WaitingPlayerData()).label = loc5.name;
                loc9.player = loc5;
                loc9.copyId = loc1.copyId;
                loc9.isCross = false;
                loc9.waitingPlayer = loc6;
                if (EntityUtil.toString(loc5.entityId) != EntityUtil.toString(cache.role.entityInfo.entityId)) 
                {
                    loc9.level = loc5.level;
                    loc4.push(loc9);
                }
                else 
                {
                    loc9.level = 1000;
                    loc4.push(loc9);
                }
                ++loc7;
            }
            loc2.sortOn("playerNum", Array.NUMERIC | Array.DESCENDING);
            loc2.sortOn("progress");
            loc4.sortOn("level", Array.DESCENDING);
            cache.copy.copyWaitingGroups = loc2;
            cache.copy.copyWaitingPlayers = loc4;
            this._copyGroupModule.updateNearByList(loc2, loc4, CopyUtil.hasPracticeNum(loc1.copyId));
            this._copyGroupModule.updateSignUpBtn();
            return;
        }

        /* internal  */onUpdateCrossWaitingInfo(arg1: SCopyWaitingInfo): void
        {
            var loc3=null;
            var loc4=null;
            var loc5=null;
            var loc6=null;
            var loc7=0;
            var loc8=null;
            var loc9=null;
            var loc1=CopyConfig.instance.getInfoByCode(arg1.copyCode);
            if (!loc1) 
            {
                return;
            }
            var loc2=[];
            var loc10=0;
            var loc11=arg1.groupInfos;
            for(loc3 of loc11) 
            {
                if (loc3.playerNum == 5) 
                {
                    continue;
                }
                (loc8 = new WaitingGroupData()).group = loc3;
                loc8.copy = loc1;
                loc8.progress = loc3.progress;
                loc8.playerNum = loc3.playerNum;
                loc8.isCross = true;
                loc2.push(loc8);
            }
            loc4 = [];
            loc7 = 0;
            while (loc7 < arg1.waitingPlayers.length) 
            {
                loc5 = (loc6 = arg1.waitingPlayers[loc7] as SWaitingPlayer).miniPlayer;
                (loc9 = new WaitingPlayerData()).label = loc5.name;
                loc9.player = loc5;
                loc9.copyId = loc1.copyId;
                loc9.isCross = true;
                loc9.waitingPlayer = loc6;
                if (EntityUtil.toString(loc5.entityId) != EntityUtil.toString(cache.role.entityInfo.entityId)) 
                {
                    loc9.level = loc5.level;
                    loc4.push(loc9);
                }
                else 
                {
                    loc9.level = 1000;
                    loc4.push(loc9);
                }
                ++loc7;
            }
            loc2.sortOn("playerNum", Array.NUMERIC | Array.DESCENDING);
            loc2.sortOn("progress");
            loc4.sortOn("level", Array.DESCENDING);
            cache.copy.crossCopyWaitingGroups = loc2;
            cache.copy.crossCopyWaitingPlayers = loc4;
            this._copyGroupModule.updateNearByList(loc2, loc4, CopyUtil.hasPracticeNum(loc1.copyId));
            this._copyGroupModule.updateSignUpBtn();
            return;
        }

        /* internal  */onCopyOperationRoomCommand(arg1: MessageBlock): void
        {
            var loc1=null;
            var loc2=null;
            loc1 = arg1.messageBase as SCopyRoomOperation;
            var loc3=loc1.operation;
            switch (loc3) 
            {
                case ECopyWaitingOperate._ECopyWaitingGather:
                {
                    loc2 = cache.scene.getNpcByNpcId(loc1.npcId);
                    if (loc2 && !NpcEffectRule.checkDistanceByCode(loc2.id)) 
                    {
                        this.copyGroupGatherPanel.updateGatherInfo(loc1.copyCode, loc1.npcId);
                        this.copyGroupGatherPanel.show();
                    }
                    break;
                }
            }
            return;
        }

        /* internal  */onGatherPanelMinimize(arg1: Object /* flash.events.Event */): void
        {
            if (!this._copyGroupGatherButton) 
            {
                this._copyGroupGatherButton = new GButton();
                this._copyGroupGatherButton.styleName = "GatherMiniButton";
                this._copyGroupGatherButton.x = UiHintIconView.instance.x - 35;
                this._copyGroupGatherButton.y = UiHintIconView.instance.y;
                this._copyGroupGatherButton.setSize(25, 25);
                this._copyGroupGatherButton.label = "";
                this._copyGroupGatherButton.addEventListener(flash.events.MouseEvent.CLICK, this.onGatherMiniBtnClick);
            }
            this.copyGroupGatherPanel.hide();
            LayerManager.uiLayer.addChild(this._copyGroupGatherButton);
            return;
        }

        /* internal  */onGatherMiniBtnClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            this.copyGroupGatherPanel.show();
            if (this._copyGroupGatherButton && this._copyGroupGatherButton.parent) 
            {
                this._copyGroupGatherButton.parent.removeChild(this._copyGroupGatherButton);
            }
            return;
        }

        /* internal  */onCopyGroupQuickApply(arg1: DataEvent): void
        {
            var loc2=null;
            var loc3=null;
            var loc4=0;
            var loc1=arg1.data as Boolean;
            if (!loc1 && cache.group.isInGroup || loc1 && cache.crossGroup.isInGroup) 
            {
                MsgManager.showRollTipsMsg(Language.getString(60030));
                return;
            }
            if (!loc1 && cache.copy.copyWaitingGroups) 
            {
                loc2 = cache.copy.copyWaitingGroups;
                loc4 = 0;
                while (loc4 < this.applyNum) 
                {
                    if (loc2.length > loc4) 
                    {
                        if ((loc3 = loc2[loc4]["group"] as SCopyGroupInfo).progress == -1) 
                        {
                            Dispatcher.dispatchEvent(new DataEvent(EventName.Group_DoApply, loc3.captainId));
                        }
                    }
                    ++loc4;
                }
            }
            else if (loc1 && cache.copy.crossCopyWaitingGroups) 
            {
                loc2 = cache.copy.crossCopyWaitingGroups;
                loc4 = 0;
                while (loc4 < this.applyNum) 
                {
                    if (loc2.length > loc4) 
                    {
                        if ((loc3 = loc2[loc4]["group"] as SCopyGroupInfo).progress == -1) 
                        {
                            Dispatcher.dispatchEvent(new DataEvent(EventName.CrossGroup_DoApply, loc3.captainId));
                        }
                    }
                    ++loc4;
                }
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.CopyGroupRefreshNearby));
            return;
        }

        /* internal  */onCopyGroupQuickInvite(arg1: DataEvent): void
        {
            var loc2=null;
            var loc3=null;
            var loc4=0;
            var loc1=arg1.data as Boolean;
            if (!loc1 && !cache.group.isInGroup || loc1 && !cache.crossGroup.isInGroup) 
            {
                MsgManager.showRollTipsMsg(Language.getString(60031));
                return;
            }
            if (!loc1 && cache.copy.copyWaitingPlayers) 
            {
                loc2 = cache.copy.copyWaitingPlayers;
                loc4 = 0;
                while (loc4 < this.inviteNum) 
                {
                    if (loc2.length > loc4) 
                    {
                        loc3 = loc2[loc4]["player"] as SPublicMiniPlayer;
                        Dispatcher.dispatchEvent(new DataEvent(EventName.Group_DoInvite, loc3.entityId));
                    }
                    ++loc4;
                }
            }
            else if (loc1 && cache.copy.crossCopyWaitingPlayers) 
            {
                loc2 = cache.copy.crossCopyWaitingPlayers;
                loc4 = 0;
                while (loc4 < this.inviteNum) 
                {
                    if (loc2.length > loc4) 
                    {
                        loc3 = loc2[loc4]["player"] as SPublicMiniPlayer;
                        Dispatcher.dispatchEvent(new DataEvent(EventName.CrossGroup_DoInvite, loc3.entityId));
                    }
                    ++loc4;
                }
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.CopyGroupRefreshNearby));
            return;
        }

        /* internal  */onCrossGroupEnterConfirm(arg1: DataEvent): void
        {
            GameProxy.copy.replyCrossCopyCost(arg1.data as int, true);
            return;
        }

        /* internal  */onCrossGroupEnterCancel(arg1: DataEvent): void
        {
            GameProxy.copy.replyCrossCopyCost(arg1.data as int, false);
            return;
        }

        /* internal  */onReceiveCostNoticeEnter(arg1: SCrossCopyCostNotice): void
        {
            this.crossCopyGroupEnterPanel.updateCostNotice(arg1, cache.crossGroup.players, cache.crossGroup.captain, CopyUtil.hasPracticeNum(arg1.copyCode));
            this.crossCopyGroupEnterPanel.show();
            return;
        }

        /* internal  */onCrossCopyCheckState(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SCrossCopyCheckState;
            if (this._crossCopyGroupEnterPanel && !this._crossCopyGroupEnterPanel.isHide) 
            {
                this._crossCopyGroupEnterPanel.updateGroupPlayersState(loc1, cache.role.entityInfo.entityId);
            }
            return;
        }

        public showModuleByCopy(arg1: TCopy, arg2: NPCInfo): void
        {
            this.copyGroupModule.resetCopyInfo(arg1, arg2);
            this.copyGroupModule.show();
            return;
        }

        public hideGroupModules(): void
        {
            if (this._copyGroupGatherButton && this._copyGroupGatherButton.parent) 
            {
                LayerManager.uiLayer.removeChild(this._copyGroupGatherButton);
            }
            if (this._copyGroupGatherPanel && !this._copyGroupGatherPanel.isHide) 
            {
                this._copyGroupGatherPanel.hide();
            }
            if (this._crossCopyGroupEnterPanel && !this._crossCopyGroupEnterPanel.isHide) 
            {
                this._crossCopyGroupEnterPanel.hide();
            }
            return;
        }

        public stageResize(): void
        {
            if (this._copyGroupGatherPanel && this._copyGroupGatherPanel.isHide) 
            {
                this._copyGroupGatherPanel.x = UiHintIconView.instance.x - 35;
                this._copyGroupGatherPanel.y = UiHintIconView.instance.y;
            }
            if (this._copyGroupGatherButton) 
            {
                this._copyGroupGatherButton.x = UiHintIconView.instance.x - 35;
                this._copyGroupGatherButton.y = UiHintIconView.instance.y;
            }
            return;
        }

        /* internal */ /* var */_copyGroupModule: ICopyGroupModuleNew;

        /* internal */ /* var */_copyGroupGatherButton: GButton;

        /* internal */ /* var */_copyGroupGatherPanel: CopyGroupGatherPanel;

        /* internal */ /* var */_crossCopyGroupEnterPanel: CrossCopyGroupEnterPanel;

        /* internal */ /* var */applyNum: int=5;

        /* internal */ /* var */inviteNum: int=10;
    }
